const db = require('./config/database')

;(async () => {
  try {
    console.log('📊 检查小说数据...')

    // 检查小说表
    const novelResult = await db.query('SELECT COUNT(*) as count FROM novels')
    console.log('小说表记录数:', novelResult[0].count)

    if (novelResult[0].count > 0) {
      const novels = await db.query(
        'SELECT id, title, view_count, favorite_count FROM novels LIMIT 3'
      )
      console.log('\n📚 示例小说:')
      novels.forEach(n =>
        console.log(
          `- ${n.title} (浏览:${n.view_count}, 收藏:${n.favorite_count || 0})`
        )
      )
    }

    // 检查章节表
    const chapterResult = await db.query(
      'SELECT COUNT(*) as count FROM chapters'
    )
    console.log('\n章节表记录数:', chapterResult[0].count)

    if (chapterResult[0].count > 0) {
      const chapters = await db.query(
        'SELECT novel_id, title FROM chapters LIMIT 3'
      )
      console.log('\n📖 示例章节:')
      chapters.forEach(c => console.log(`- ${c.title} (小说ID:${c.novel_id})`))
    }

    if (novelResult[0].count === 0) {
      console.log('\n⚠️  数据库中没有小说数据，首页使用的是mock数据')
    } else {
      console.log('\n✅ 数据库中有小说数据')
    }
  } catch (error) {
    console.error('❌ 数据库查询失败:', error.message)
    console.log('\n可能的原因:')
    console.log('1. 数据库未连接')
    console.log('2. 表不存在')
    console.log('3. 权限问题')
  }

  process.exit()
})()
